package lm.concurrency.day2;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @Auther: Min.Liu
 * @Description:
 * @Date: Created in 15:18 2025/4/11
 */
public class StringTest {
    private static String s1 = "...";
    private static AtomicReference<String> s2 = new AtomicReference<>(s1);
    
    static class Student {
        private static String base = "变动";
        private String name;
        private int age;

        public static String getBase() {
            return base;
        }

        public static void setBase(String base) {
            Student.base = base;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    public static void main(String[] args) {
        test1();
    }


    private static void test1() {
        ArrayList<Thread> list = new ArrayList<>();
        for (int i = 0; i < 100 ; i++) {
            int finalI = i;
            Thread thread = new Thread(() -> {
                s1 += "."+ finalI;
            });
            thread.start();
            list.add(thread);
        }
        for (Thread thread : list) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(s1.length());
        System.out.println(s1);
        for (int i = 0; i < 100 ; i++) {
            if (!s1.contains("."+i)) {
                System.out.println(i+"没有加上");
            }
        }
    }

    private static void test2() {
        ArrayList<Thread> list = new ArrayList<>();
        for (int i = 0; i < 100 ; i++) {
            int finalI = i;
            Thread thread = new Thread(() -> {
                //s1 += "."+ finalI;
                s2.getAndUpdate(s->s += "."+ finalI);
            });
            thread.start();
            list.add(thread);
        }
        for (Thread thread : list) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(s2.get().length());
        System.out.println(s2.get());
        for (int i = 0; i < 100 ; i++) {
            if (!s2.get().contains("."+i)) {
                System.out.println(i+"没有加上");
            }
        }
    }

    private static void baseTest() {
        Student student = new Student();
        Student newStu = student;
        student.setName("liumin");
        student.setAge(13);
        newStu.setName("刘淑仪");
        System.out.println(student);
        System.out.println(newStu);
        System.out.println(student==newStu);
    }
}
